
/* GCSx
** SPRITE.CPP
**
** Sprite support (no edit-specific version)
*/

/*****************************************************************************
** Copyright (C) 2003-2006 Janson
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
*****************************************************************************/

#include "all.h"

// @TODO: respect ref

Sprite::Sprite(int myId) { start_func
    id = myId;
    texture = NULL;
    origImage = NULL;
    sourceTileSet = NULL;
    sourceAnimGroup = NULL;
    obj = NULL;
    varTable[PROP_ORIGIN_X].i = varTable[PROP_ORIGIN_Y].i = 0;
    varTable[PROP_X].i = varTable[PROP_Y].i = 0;
    active = 0;
    varTable[PROP_PRIORITY].i = DEFAULT_PRIORITY;
    
    members = varTable;
    ref = 0;
    subtype = SUB_SPRITE;
}

Sprite::~Sprite() { start_func
    // @TODO: assert ref=0?
    setImage();
}

void Sprite::activateResource() { start_func
    if (sourceTileSet) {
        // @TODO: throw_File
        sourceTileSet->markLockPlay();
        texture = sourceTileSet->getTexture();
        texGraphic = tileNum;
    }
    if (sourceAnimGroup) {
        // @TODO:
    }
    if (origImage) {
        // @TODO:
    }
}

void Sprite::deactivateResource() { start_func
    if (sourceTileSet) {
        sourceTileSet->markUnlockPlay();
        texture = NULL;
    }
    if (sourceAnimGroup) {
        // @TODO:
        texture = NULL;
    }
    if (origImage) {
        // @TODO:
        texture = NULL;
    }
}

void Sprite::setActive() { start_func
    if (!active) {
        activateResource();
        active = 1;
    }
}

void Sprite::setInactive() { start_func
    if (active) {
        deactivateResource();
        active = 0;
    }
}

void Sprite::setImage(class TileSet* tileset, int tile) { start_func
    assert(tileset);
    setImage();
    if ((tile > 0) && (tile <= tileset->getCount())) {
        sourceTileSet = tileset;
        tileNum = tile;
        width = tileset->getWidth();
        height = tileset->getHeight();
        if (active) activateResource();
    }
}

void Sprite::setImage(AnimGroup* animgroup, int anim, int frame) { start_func
    assert(animgroup);
    setImage();
    if ((anim > 0) && (anim <= animgroup->getCount())) {
        sourceAnimGroup = animgroup;
        animNum = anim;
        animFrame = frame;
        // width = @TODO:
        // height = @TODO:
        if (active) activateResource();
    }
}

void Sprite::setImage() { start_func
    if (active) deactivateResource();
    if (sourceTileSet) {
        // sourceTileSet->markUnlock();
        sourceTileSet = NULL;
    }
    if (sourceAnimGroup) {
        // sourceAnimGroup->markUnlock();
        sourceAnimGroup = NULL;
    }
    if (origImage) {
        SDL_FreeSurface(origImage);
        origImage = NULL;
    }
    assert(!texture);
}

void Sprite::draw(int vX, int vY) { start_func
    if (!texture) return;

    // This clipping can't be used as-is for rotated/enlarged sprites
    int dX = varTable[PROP_X].i + varTable[PROP_ORIGIN_X].i - vX;
    if (dX >= screenWidth) return;
    if (dX + width < 0) return;

    int dY = varTable[PROP_Y].i + varTable[PROP_ORIGIN_Y].i - vY;
    if (dY >= screenHeight) return;
    if (dY + height < 0) return;

    // @TODO: don't set color if same as last one
    // (have a function that says "reset color optimization" called
    // right before doing a round of sprites)
    glColor4f(1.0f, 1.0f, 1.0f, 1.0f);    
    texture->draw(tileNum, dX, dY);
}
